package com.le.tester.imooc.oldfairy.coding.datastructure;

/**
 * createTime：2023/5/29 15:33
 * description：八皇后和搜索
 */
public class EQueen {


    int[] queues = new int[8];


    //列开始分解，然后到行
    public boolean solve(int col) {
        //八行已经全部占满
        if (col == 8) {
            return true;
        }
        for (int i = 0; i < 8; i++) {

            queues[col] = i;

            boolean flag = true;

            for (int j = 0; j < col; j++) {
                //行差和列差
                int rowDiff = Math.abs(queues[j] - i);
                int colDiff = col - j;

                //对角线和平行线过滤
                if (queues[j] == i || rowDiff == colDiff) {
                    flag = false;
                    break;
                }
            }

            if (flag && solve(col + 1)) {
                return true;
            }

        }
        return false;
    }

    void print() {

        for (int i = 0; i < 8; i++) {

            for (int j = 0; j < 8; j++) {
                if (queues[i] == j) {
                    System.out.print("Q");
                } else {

                    System.out.print(".");
                }

            }
            System.out.print("\n");
        }
    }

    public static void main(String[] args) {
        EQueen eQueen = new EQueen();

        eQueen.solve(0);
        eQueen.print();
    }


}
